#include "umsusers.h"


UMSUsers::UMSUsers(UMSDatabase *dbase)
{
	_dbase = dbase;
}


QMap<QString,QString> UMSUsers::users()
{
	QMap<QString,QString> users;
	if(_dbase==0) return users;
	_dbase->exec("select \"login\" from \"umsusers\"");
	while(_dbase->next()){
		users[_dbase->uin()] =	_dbase->value("login");
	}
	return users;
}

QStringList UMSUsers::databases(const QString &uname)
{
	if(_dbase==0) return QStringList();
	_dbase->exec(QString("select \"db_priv\" from \"umsusers\" where \"login\"=\"%1\"").arg(uname));
	if(!_dbase->next()) return QStringList();
	return _dbase->values("db_priv");
}

QStringList UMSUsers::addrs(const QString &uname)
{
	if(_dbase==0) return QStringList();
	_dbase->exec(QString("select \"host_priv\" from \"umsusers\" where \"login\"=\"%1\"").arg(uname));
	if(!_dbase->next()) return QStringList();
	return _dbase->values("host_priv");
}

void UMSUsers::newUser(const QString &uname)
{
	if(_dbase==0) return;
	_dbase->exec(QString("insert into \"umsusers\" values \"login\"=\"%1\"").arg(uname));
	if(!_dbase->next()){
		qWarning() << Q_FUNC_INFO << _dbase->lastError();
	}
}

void UMSUsers::removeUser(const QString &uname)
{
	if(_dbase==0) return;		
	_dbase->exec(QString("delete from \"umsusers\" where \"login\"=\"%1\"").arg(uname));
	if(!_dbase->next()){
		qWarning() << Q_FUNC_INFO << _dbase->lastError();
	}	
}

void UMSUsers::newUser(User *u)
{
	if(_dbase==0) return;		
/*insert into "umsusers" values 
"login"="m1",
"MD5passwd"="",
"fio_group"=""
{
	"lastname"="",
	"firstname"="",
	"parentname"=""
},
"certificate"="",
"host_priv"="h1",
"host_priv"="h2",
"db_priv"=""	*/
	QCryptographicHash hash(QCryptographicHash::Md5);
	hash.addData(u->passwd.toUtf8());
	
	QString passwd = hash.result().toHex();
	QString hosts,bases;
	foreach(QString host,u->hosts){
		hosts += QString(",\"host_priv\"=\"%1\"").arg(host);
	}
	foreach(QString base,u->bases){
		bases += QString(",\"db_priv\"=\"%1\"").arg(base);
	}
	//hosts = u->hosts.join(
	
	QString query = QString("insert into \"umsusers\" values \"login\"=\"%1\",\"MD5passwd\"=\"%2\",\"fio_group\"=\"1\"\
	{\
	\"lastname\"=\"%3\",\
	\"firstname\"=\"%4\",\
	\"parentname\"=\"%5\"\
	},\
	\"certificate\"=\"%6\"\
	%7\
	%8\
	").arg(u->login).arg(passwd).arg(u->fam).arg(u->name).arg(u->otch).arg(u->cert).arg(hosts).arg(bases);
	//qDebug() << Q_FUNC_INFO << query;
	_dbase->exec(query);
	if(!_dbase->next()){
		qWarning() << Q_FUNC_INFO << _dbase->lastError();
	}	

	
}

QString UMSUsers::selectCrd(const QString &crdname,const QString &uname)
{
	_dbase->exec(QString("select \"%1\" from \"umsusers\" where \"login\"=\"%2\"").arg(crdname).arg(uname));
	if(!_dbase->next()) return QString();		
	return _dbase->value(crdname);
}

User *UMSUsers::user(const QString &uname)
{
	if(_dbase==0) return 0;
	_dbase->exec(QString("select * from \"umsusers\" where \"login\"=\"%1\"").arg(uname));
	if(!_dbase->next()) return 0;
	User *u = new User;
	
	u->login = uname;
	u->passwd = "12345678";
	u->cert = _dbase->value("certificate");
	u->fam  = selectCrd("lastname",uname);
	u->name = selectCrd("firstname",uname);
	u->otch = selectCrd("parentname",uname);
	u->bases = _dbase->values("db_priv");
	u->hosts = _dbase->values("host_priv");
	return u;	
}
